//
//  View_Tool.m
//  news_ios_app
//
//  Created by fank on 16/3/19.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "View_Tool.h"
#import "DefineConstant.h"

#define kAlertDelayTime     1.0f

@implementation View_Tool
+ (void)alert:(NSString *)message type:(AlertType)type autoHide:(BOOL)isAutoHide inView:(UIView *)view
{
    [self alert:message type:type autoHide:isAutoHide inView:view durationTime:kAlertDelayTime];
}

+ (void)alert:(NSString *)message type:(AlertType)type autoHide:(BOOL)isAutoHide inView:(UIView *)view durationTime:(NSTimeInterval)duration
{
    [self alert:message type:type autoHide:isAutoHide inView:view durationTime:duration alertImage:nil];
}

+ (void)alert:(NSString *)message type:(AlertType)type autoHide:(BOOL)isAutoHide inView:(UIView *)view durationTime:(NSTimeInterval)duration alertImage:(UIImage *)alertImage
{
    NSString *msg = @"";
    if (IsNilOrNull(message) || [message isEqualToString:@""]) {
        msg = NSLocalizedString(@"loading...", nil);
    }else{
        msg = message;
    }
    
    if (view == nil) {
        view = [[UIApplication sharedApplication] keyWindow];
    }
    
    [self hideAlertInView:view];
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    hud.detailsLabelFont = [UIFont systemFontOfSize:12.0f];
    hud.detailsLabelText = msg;
    hud.yOffset = -60.0f;
    hud.removeFromSuperViewOnHide = YES;
    
    NSString *alertImageName = @"";
    MBProgressHUDMode mode = MBProgressHUDModeCustomView;
    if (type == AlertTypeFail) {
        alertImageName = @"shared_alert_fail";
    }
    else if (type == AlertTypeSuccess) {
        alertImageName = @"shared_alert_success";
    }
    else if (type == AlertTypeNetwork) {
        alertImageName = @"shared_alert_network";
    }
    else if (type == AlertTypeMessage) {
        alertImageName = @"";
    }
    else if (type == AlertTypeWait) {
        mode = MBProgressHUDModeIndeterminate;
    }
    
    if (type == AlertTypeCustom) {
        hud.customView = [[UIImageView alloc] initWithImage:alertImage];
    } else  if (mode == MBProgressHUDModeCustomView){
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:alertImageName]];
    }
    hud.mode = mode;
    
    [view addSubview:hud];
    [hud show:YES];
    if (isAutoHide) {
        double delayInSeconds = (duration > 0 ? duration : kAlertDelayTime);
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [hud hide:YES];
        });
    }
}

+ (void)alert:(NSString *)message type:(AlertType)type autoHide:(BOOL)isAutoHide
{
    if (IsStrEmpty(message))
    {
        message = nil;
    }
    [self alert:message type:type autoHide:isAutoHide inView:[[UIApplication sharedApplication] keyWindow]];
}

+ (void)alert:(NSString *)message type:(AlertType)type durationTime:(NSTimeInterval)time
{
    [self alert:message type:type autoHide:YES inView:[[UIApplication sharedApplication] keyWindow] durationTime:time];
}

+ (void)alert:(NSString *)message type:(AlertType)type
{
    [self alert:message type:type autoHide:YES];
}

+ (void)alertMessage:(NSString *)message
{
    [self alert:message type:AlertTypeMessage];
}

+ (void)alertSuccess:(NSString *)message
{
    [self alert:message type:AlertTypeSuccess];
}

+ (void)alertFail:(NSString *)message
{
    [self alert:message type:AlertTypeFail];
}


+ (void)alertNetwork:(NSString *)message
{
    [self alert:message type:AlertTypeNetwork];
}

+ (void)alertNetworkLess
{
    [self alertNetwork:NSLocalizedString(@"bad network try later", nil)];
}

+ (void)alertWait
{
    [self alert:NSLocalizedString(@"loading...", nil) type:AlertTypeWait autoHide:NO];
}

+ (void)hideAlert
{
    [MBProgressHUD hideAllHUDsForView:[[UIApplication sharedApplication] keyWindow] animated:NO];
}

+ (void)hideAlertInView:(UIView *)view
{
    [MBProgressHUD hideAllHUDsForView:view animated:NO];
}

+ (void)alertCustomView:(UIImage *)alertImage message:(NSString *)message durationTime:(NSTimeInterval)time
{
    [self alert:message type:AlertTypeCustom autoHide:YES inView:[[UIApplication sharedApplication] keyWindow] durationTime:time alertImage:alertImage];
}
@end
